package com.cy.sc.controller;

import com.cy.sc.pojo.Dept;
import com.cy.sc.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private IDeptService iDeptService;
    //客户端服务
    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/add")
    public boolean addDept(Dept dept) {
        return iDeptService.addDept(dept);
    }

    @GetMapping("/get/{id}")
    public Dept getDeptById(@PathVariable("id") long id) {

        return iDeptService.getDeptById(id);
    }

    @GetMapping("/list")
    public List<Dept> getDeptAll() {
        return iDeptService.getDeptAll();
    }
    //***示例***
    //通过Eureka客户端对象 获取eureka 服务信息
    @GetMapping("/discovery")
    public Object discovery() {
        //获取服务清单
        List<String> services = discoveryClient.getServices();
        System.out.println("services=>" + services);
        //得到一个具体的微服务信息，通过具体服务id(ApplicationName)
        List<ServiceInstance> instances = discoveryClient.getInstances("SERVER-SERVICE-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost() + "\t" +
                            instance.getPort() + "\t" +
                            instance.getUri() + "\t" +
                            instance.getServiceId()
            );
        }
        return this.discoveryClient;
    }
}
